package com.woniuxy.micromallserver.categorymag.mapper;

import com.woniuxy.micromallserver.bean.Category;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author Chan
 * @Date 2021/1/21 14:37
 */
@Mapper
public interface CategoryMapper {

    /**
     * 分页查询所有品类
     * @return
     * */
    @Select(value = {" <script>" +
            " SELECT * FROM category " +
            " <where> 1=1 " +
            " <if test=\"category.categoryName != null and category.categoryName != ''\"> AND category_name like concat('%', #{category.categoryName}, '%') </if> " +
            " <if test=\"category.beginCreateTime != null and category.beginCreateTime != '' and category.endCreateTime != null and category.endCreateTime != ''\" >  AND date_format(create_time,'%Y-%m-%d') between #{category.beginCreateTime} and #{category.endCreateTime}</if> " +
            " </where>" + "and category_status = 1 order by category_order" +
            " </script>"})
    @Results({
            @Result(property = "categoryId", column = "category_id"),
            @Result(property = "categoryName", column = "category_name"),
            @Result(property = "categoryOrder", column = "category_order"),
            @Result(property = "subcategory", column = "subcategory"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "categoryDescription", column = "category_description"),
            @Result(property = "categoryStatus", column = "category_status")
    })
    List<Category> getCategoriesByPage(@Param("category") Category category);

    /**
     * 根据id查询分类
     * @return 返回所有分类
     * */
    @ResultType(Category.class)
    @Select("select * from category where category_id = #{categoryId}")
    @Results({
            @Result(property = "categoryId", column = "category_id"),
            @Result(property = "categoryName", column = "category_name"),
            @Result(property = "categoryOrder", column = "category_order"),
            @Result(property = "subcategory", column = "subcategory"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "categoryDescription", column = "category_description"),
            @Result(property = "categoryStatus", column = "category_status")
    })
    Category getCategoryById(@Param("categoryId") Integer categoryId);


    /**
     * 查询所有分类并按照顺序排列
     * @return 返回所有分类
     * */
    @ResultType(Category.class)
    @Select("select * from category order by category_order")
    @Results({
            @Result(property = "categoryId", column = "category_id"),
            @Result(property = "categoryName", column = "category_name"),
            @Result(property = "categoryOrder", column = "category_order"),
            @Result(property = "subcategory", column = "subcategory"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "categoryDescription", column = "category_description"),
            @Result(property = "categoryStatus", column = "category_status")
    })
    List<Category> getAllCategories();


    /**
     * 多条件查询分类
     * @param category
     * @return 返回所有分类
     * */
    @ResultType(Category.class)
    @Select(value = {" <script>" +
            " SELECT * FROM category " +
            " <where> 1=1 " +
            " <if test=\"category.categoryName != null and category.categoryName != ''\"> AND category_name like concat('%', #{category.categoryName}, '%') </if> " +
            " <if test=\"category.beginCreateTime != null and category.beginCreateTime != '' and category.endCreateTime != null and category.endCreateTime != ''\" >  AND date_format(create_time,'%Y-%m-%d') between #{category.beginCreateTime} and #{category.endCreateTime}</if> " +
            " </where>" +
            " </script>"})
    @Results({
            @Result(property = "categoryId", column = "category_id"),
            @Result(property = "categoryName", column = "category_name"),
            @Result(property = "categoryOrder", column = "category_order"),
            @Result(property = "subcategory", column = "subcategory"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "categoryDescription", column = "category_description"),
            @Result(property = "categoryStatus", column = "category_status")
    })
    List<Category> getCategoriesByMultiConditions(@Param("category") Category category);

    /**
     * 编辑分类
     * @param categoryId
     * @param categoryName
     * @param categoryOrder
     * @param subcategory
     * @param categoryDescription
     * @return int
     * */
    @Update(value = {"<script>" +
            "     update category  \n" +
            "     <set>  \n" +
            "       <if test=\"categoryName != null\">  \n" +
            "         category_name = #{categoryName},  \n" +
            "       </if>  \n" +
            "       <if test=\"categoryOrder != null\">  \n" +
            "         category_order = #{categoryOrder},  \n" +
            "      </if>  \n" +
            "       <if test=\"subcategory != null\">  \n" +
            "         subcategory = #{subcategory},  \n" +
            "      </if>  \n" +
            "       <if test=\"categoryDescription != null\">  \n" +
            "         category_description = #{categoryDescription},  \n" +
            "      </if>  \n" +
            "     </set> " +
            "     where category_id = #{categoryId}" +
            "      </script>"
    })
    int updateCategory(@Param("categoryId") int categoryId, @Param("categoryName") String categoryName,
                       @Param("categoryOrder") int categoryOrder,
                       @Param("subcategory") String subcategory, @Param("categoryDescription") String categoryDescription);

    /**
     * 下架、删除指定分类
     * @param categoryId
     * @param categoryStatus
     * @return
     * */
    @Update("update category set category_status = #{categoryStatus} where category_id = #{categoryId}")
    int changeCategoryStatus(@Param("categoryId") int categoryId, @Param("categoryStatus") int categoryStatus);

    /**
     * 新增分类
     * @param category
     * @return
     * */
    @Insert("insert into category values (#{category.categoryId},#{category.categoryName},#{category.categoryOrder}," +
            "#{category.subcategory},#{category.createTime},#{category.categoryDescription},1)")
    int addCategory(@Param("category") Category category);


}
